/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.accountinfo.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.accountinfo.entity.AccountInfo;
import com.thinkgem.jeesite.modules.accountinfo.service.AccountInfoService;

/**
 * 用户注册Controller
 * @author ljh
 * @version 2016-12-12
 */
@Controller
@RequestMapping(value = "${adminPath}/accountInfo")
public class AccountInfoController extends BaseController {

	@Autowired
	private AccountInfoService accountInfoService;
	
	@ModelAttribute
	public AccountInfo get(@RequestParam(required=false) String id) {
		AccountInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = accountInfoService.get(id);
		}
		if (entity == null){
			entity = new AccountInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("accountInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(AccountInfo accountInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AccountInfo> page = accountInfoService.findPage(new Page<AccountInfo>(request, response), accountInfo); 
		model.addAttribute("page", page);
		return "modules/account/admin/accountInfoList";
	}

	@RequiresPermissions("accountInfo:view")
	@RequestMapping(value = "form")
	public String form(AccountInfo accountInfo, Model model) {
		model.addAttribute("accountInfo", accountInfo);
		return "modules/account/admin/accountInfoForm";
	}

	@RequiresPermissions("accountInfo:edit")
	@RequestMapping(value = "save")
	public String save(AccountInfo accountInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, accountInfo)){
			return form(accountInfo, model);
		}
		accountInfoService.save(accountInfo);
		addMessage(redirectAttributes, "保存用户成功");
		return "redirect:"+Global.getAdminPath()+"/accountInfo/list?repage";
	}
	
	@RequiresPermissions("accountInfo:edit")
	@RequestMapping(value="disable")
    public String disableUser(String id,Boolean disableStatus,RedirectAttributes redirectAttributes){

        AccountInfo entity = accountInfoService.get(id);
        if(disableStatus){
            entity.setUserStatus(1);

            addMessage(redirectAttributes, "取消禁用成功");
        }else{
            entity.setUserStatus(0);

            addMessage(redirectAttributes, "禁用用户成功");
        }
        
        accountInfoService.save(entity);
        return "redirect:"+Global.getAdminPath()+"/accountInfo/list?repage";
    }
	
	@RequiresPermissions("accountInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(AccountInfo accountInfo, RedirectAttributes redirectAttributes) {
		accountInfoService.delete(accountInfo);
		addMessage(redirectAttributes, "删除用户成功");
		return "redirect:"+Global.getAdminPath()+"/account/admin/?repage";
	}

}